package cn.com.zero.front.auth.security.service.impl;

import cn.com.zero.api.message.dubbo.IMessageService;
import cn.com.zero.base.util.RegexUtil;
import cn.com.zero.base.util.SensitiveInfoUtil;
import cn.com.zero.common.auth.security.po.AuthUserPO;
import cn.com.zero.common.auth.security.po.ErrorLoginLogPO;
import cn.com.zero.common.auth.security.po.LoginLogPO;
import cn.com.zero.api.auth.po.MenuPO;
import cn.com.zero.common.auth.security.service.AbstractLoginAuthenticationService;
import cn.com.zero.front.auth.constant.FrontAuthConstants;
import cn.com.zero.front.auth.mapper.IUserLoginMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description 登录认证需要的service处理实现类
 * @createTime 2023/3/23 17:15
 */
@Component
public class LoginAuthenticationService extends AbstractLoginAuthenticationService {
    private IMessageService messageService;
    private IUserLoginMapper userLoginMapper;


    @Override
    public String getAuthenticationSide() {
        return FrontAuthConstants.FRONT_SIDE_NAME;
    }

    @Override
    public AuthUserPO getUserByMobile(String mobile) {
        String encryptMobile = SensitiveInfoUtil.encryptMobile(mobile);
        AuthUserPO userPO = userLoginMapper.getUserByMobile(encryptMobile);
        if (userPO == null) {
            return null;
        }
        userPO.setMobile(mobile);
        return userPO;
    }

    @Override
    public AuthUserPO getUserByNameOrMobile(String userName) {
        boolean is_Mobile = RegexUtil.validateMobile(userName);
        if (is_Mobile) {
            return getUserByMobile(userName);
        }
        AuthUserPO userPO = userLoginMapper.getUserByName(userName);
        if (userPO == null) {
            return null;
        }
        if (StringUtils.hasLength(userPO.getMobile())) {
            String mobile = SensitiveInfoUtil.decryptMobile(userPO.getMobile());
            userPO.setMobile(mobile);
        }
        return userPO;
    }

    @Override
    public List<MenuPO> getMenusByUserId(String userId) {
        return null;
    }

    @Override
    public Long checkLoginVerificationCode(String mobile, String verificationCode) {
        return messageService.checkVerificationCode(mobile,
                FrontAuthConstants.CODE_MESSAGE_TYPE_LOGIN_VERIFICATION, verificationCode);
    }


    @Override
    protected void modifyUserInfo(AuthUserPO userPO) {
        userLoginMapper.modifyUserInfo(userPO);
    }

    @Override
    protected void deleteErrorLoginLog(String userId) {
        userLoginMapper.deleteErrorLoginLog(userId);
    }

    @Override
    protected void addUserLoginLog(LoginLogPO loginLogPO) {
        userLoginMapper.addUserLoginLog(loginLogPO);
    }

    @Override
    protected int getErrorLoginTimes(String userId) {
        return userLoginMapper.getErrorLoginTimes(userId);
    }

    @Override
    protected void addUserErrorLoginLog(ErrorLoginLogPO errorLoginLogPO) {
        userLoginMapper.addUserErrorLoginLog(errorLoginLogPO);
    }

    @DubboReference
    public void setMessageService(IMessageService messageService) {
        this.messageService = messageService;
    }

    @Autowired
    public void setUserLoginMapper(IUserLoginMapper userLoginMapper) {
        this.userLoginMapper = userLoginMapper;
    }
}
